home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / HOSTATUS.C < prev    next >
C/C++ Source or Header  |  1993-04-10  |  6KB  |  186 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    h o s t a t u s . c                                             */
  3. /*                                                                    */
  4. /*    Load host status information for UUPC/extended                  */
  5. /*                                                                    */
  6. /*    Copyright (c) 1991, Andrew H. Derbyshire                        */
  7. /*--------------------------------------------------------------------*/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <time.h>
  12.  
  13. #include <io.h>
  14.  
  15. #include "lib.h"
  16. #include "hlib.h"
  17. #include "hostable.h"
  18. #include "hostatus.h"
  19. #include "security.h"
  20. #include "timestmp.h"
  21. #include "stater.h"
  22.  
  23. /*--------------------------------------------------------------------*/
  24. /*        Define current file name for panic() and printerr()         */
  25. /*--------------------------------------------------------------------*/
  26.  
  27. currentfile();
  28.  
  29. /*--------------------------------------------------------------------*/
  30. /*                          Global variables                          */
  31. /*--------------------------------------------------------------------*/
  32.  
  33. time_t start_stats = 0;
  34. time_t hstatus_age = -2;
  35.  
  36. void HostStatus( void )
  37. {
  38.  
  39.    char fname[FILENAME_MAX];
  40.    char buf[BUFSIZ];
  41.    struct HostTable *host;
  42.    FILE *stream;
  43.    unsigned short len1;
  44.    unsigned short len2;
  45.    long size;
  46.  
  47.    time_t age;
  48.  
  49.    mkfilename( fname, E_confdir, DCSTATUS );
  50.  
  51. /*--------------------------------------------------------------------*/
  52. /*    If the file does not exist but does exist in the spool          */
  53. /*    directory, attempt to move it, else use the spool directory.    */
  54. /*    If the file doesn't exist at all, return quietly.               */
  55. /*--------------------------------------------------------------------*/
  56.  
  57.    if ( access( fname, 0 ))
  58.    {
  59.       mkfilename( buf, E_spooldir, DCSTATUS );
  60.       if ( access( buf, 0 ))
  61.          return;
  62.  
  63.       if (rename( buf, fname ))
  64.       {
  65.          printerr( buf );
  66.          strcpy( fname, buf );
  67.       }
  68.    }
  69.  
  70. /*--------------------------------------------------------------------*/
  71. /*              Get the age of the file to avoid reloads              */
  72. /*--------------------------------------------------------------------*/
  73.  
  74.    age = stater( fname , &size );
  75.  
  76.    if ( age == hstatus_age )  /* Unchanged file?                     */
  77.       return;                 /* Yes --> Just return quietly         */
  78.  
  79. /*--------------------------------------------------------------------*/
  80. /*                       Actually open the file                       */
  81. /*--------------------------------------------------------------------*/
  82.  
  83.    if ((stream  = FOPEN(fname , "r", BINARY_MODE)) == NULL)
  84.    {
  85.  
  86.       perror( fname );
  87.       printmsg(1,"HostStatus: Unable to open host status file");
  88.       time(&start_stats);
  89.       return;
  90.  
  91.    } /* if */
  92.  
  93. /*--------------------------------------------------------------------*/
  94. /*                    Read the header information                     */
  95. /*--------------------------------------------------------------------*/
  96.  
  97.    fread( &len1, sizeof len1, 1, stream );
  98.    fread( &len2, sizeof len2, 1, stream );
  99.    fread( buf , 1, len1, stream);
  100.    buf[len1++] = ' ';
  101.    fread( buf + len1 , 1, len2, stream);
  102.    buf[ len1 + len2 ] = '\0';
  103.    fread( &start_stats , sizeof start_stats , 1,  stream);
  104.  
  105.    printmsg(5,"HostStatus: %s generated by %s beginning %s",
  106.          fname,
  107.          buf,
  108.          ctime(&start_stats));
  109.  
  110.    if ( start_stats < 630720000L )  /* Invalid date?              */
  111.       time(&start_stats);           /* Yes --> reset date to today*/
  112.  
  113. /*--------------------------------------------------------------------*/
  114. /*                           Load each host                           */
  115. /*--------------------------------------------------------------------*/
  116.  
  117.    while  (!feof( stream ) && !ferror(stream))
  118.    {
  119.       if (fread( &len1, sizeof len1, 1, stream ) < 1)
  120.          break;
  121.  
  122.       fread( &len2, sizeof len2, 1, stream );
  123.       fread( buf , sizeof host->hostname[0], len1, stream);
  124.       buf[ len1 ] = '\0';
  125.  
  126.       printmsg(5,"HostStatus: Name length %d, status length %d, "
  127.                  "host name \"%s\"",
  128.                  len1, len2, buf );
  129.  
  130.       host = checkreal( buf );
  131.  
  132.       if ( host == BADHOST )
  133.       {
  134.          printmsg(1,"HostStatus: Host \"%s\" not found, purging entry",
  135.                      buf );
  136.          fread( buf , len2, 1,  stream);
  137.       }
  138.       else if ( len2 <= (sizeof *(host->hstats)))
  139.       {
  140.          struct HostStats stats;    /* host status, as defined by hostatus */
  141.  
  142.          fread( &stats , len2, 1,  stream);
  143.  
  144.          printmsg(5,"HostStatus: Loaded status for host %s",
  145.                   host->hostname);
  146.  
  147. /*--------------------------------------------------------------------*/
  148. /*        Update the host status to the best known information        */
  149. /*--------------------------------------------------------------------*/
  150.  
  151.          if ( host->hstats->lconnect > stats.lconnect )
  152.             stats.lconnect = host->hstats->lconnect;
  153.  
  154.          if ( host->hstats->ltime > stats.ltime )
  155.             stats.ltime = host->hstats->ltime;
  156.          else if ( host->hstats->lconnect > stats.ltime )
  157.             ; /* No operation */
  158.          else if (( stats.save_hstatus >= nocall ) &&
  159.                   ( stats.save_hstatus < last_status ))
  160.             host->hstatus = stats.save_hstatus;
  161.          else
  162.             printmsg(0,"HostStatus: Invalid status (%d) ignored for \"%s\"",
  163.                stats.save_hstatus,host->hostname ) ;
  164.  
  165.          memcpy( host->hstats, &stats , sizeof stats );
  166.       }
  167.       else {
  168.          printmsg(5,"HostStatus: Bad record length %d (wanted %d), "
  169.                     "purging status for host %s",
  170.                     len2, (int) (sizeof *(host->hstats)), buf);
  171.          fread( buf , len2, 1,  stream);
  172.       } /* else */
  173.    } /* while */
  174.  
  175.    if (ferror( stream ))
  176.    {
  177.       printerr( fname );
  178.       clearerr( stream );
  179.    }
  180.  
  181.    fclose( stream );
  182.  
  183.    hstatus_age = age;             /* Remember the age of the file     */
  184.  
  185. } /* HostStatus */
  186.